/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.server.rpc.impl;

import java.util.List;
import java.util.logging.Logger;
import java.util.logging.Level;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.niiuzu.server.util.RssDataRetriever;
import com.niiuzu.shared.exception.ServiceRpcException;
import com.niiuzu.shared.model.RssChannel;
import com.niiuzu.shared.model.RssEntry;
import com.niiuzu.shared.rpc.FeedService;

/**
 * Implémentation du service dédié aux flux.
 */
public class FeedServiceImpl extends RemoteServiceServlet implements
		FeedService {

	private static final long serialVersionUID = -8128897257688261021L;

	private static final Logger LOGGER = Logger.getLogger(FeedServiceImpl.class
			.getName());

	@Override
	public RssEntry[] recoverFeedEntries(RssChannel[] channels)
			throws ServiceRpcException {

		RssEntry[] entries = null;

		try {

			List<RssEntry> entryList = getDataRetriever()
					.getEntriesFromChannels(channels);
			entries = entryList.toArray(new RssEntry[entryList.size()]);

		} catch (Throwable e) {
			String msg = "Erreur lors de la récupération de données";
			LOGGER.log(Level.SEVERE, msg, e);
			throw new ServiceRpcException(msg, e);
		}

		return entries;
	}

	@Override
	public RssChannel createChannelFromLink(String channelLink)
			throws ServiceRpcException {

		RssChannel channel = null;

		try {

			channel = getDataRetriever().createChannelFromLink(channelLink);

		} catch (Throwable e) {
			String msg = "Erreur lors de récupération des informations sur un flux";
			LOGGER.log(Level.SEVERE, msg, e);
			throw new ServiceRpcException(msg, e);
		}

		return channel;
	}

	/**
	 * Moteur de récupération des données de flux RSS.
	 */
	public RssDataRetriever getDataRetriever() {
		if (mockDataRetriever != null) {
			return mockDataRetriever;
		}
		return new RssDataRetriever();
	}

	/* ********************************************************************** */
	/* ********************************************************************** */
	/* ********************************************************************** */

	private RssDataRetriever mockDataRetriever;

	/**
	 * JUnit - Définit un mock pour {@link RssDataRetriever}.
	 */
	void setMockRssDataRetriever(RssDataRetriever rssDataRetriever) {
		this.mockDataRetriever = rssDataRetriever;
	}

}
